package com.han.controllers;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import com.han.bom.Exam;
import com.han.bom.Professor;
import com.han.bom.Question;
import com.han.dao.AnswerDAO;
import com.han.dao.ExamDAO;
import com.han.dao.QuestionDAO;
import com.han.dao.UserDAO;

/**
 * Servlet implementation class ProfessorViewPracticeServlet
 */
@WebServlet("/Professor/ProfessorViewExamServlet")
public class ProfessorViewExamServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	private static Logger logger = Logger.getLogger(ProfessorViewExamServlet.class.getName());
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ProfessorViewExamServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		logger.info("Entered ProfessorViewExamServlet GET");

		ExamDAO examdao = new ExamDAO();
		QuestionDAO questiondao = new QuestionDAO();
		Professor professor = null;
		UserDAO userdao = new UserDAO();
		HttpSession session = request.getSession();
		ArrayList<Exam> exams = null;
		ArrayList<Question> questions = null;
		String type = null;
		AnswerDAO answerdao = new AnswerDAO();
		ArrayList<ArrayList<String>> categories = new ArrayList<ArrayList<String>>();
		
		if(session.getAttribute("professor")!=null){
			professor = (Professor)session.getAttribute("professor");
		}
		else{
			
		}
		
		if(request.getParameter("type")!=null){
			type = request.getParameter("type");
		}
		else{
			
		}
		
		try {
			exams = examdao.retrieveExamsByProfessor(professor.getUserID());
			questions = questiondao.retrieveAllQuestions();
			
			for(Question question:questions){
				ArrayList<String> choices = answerdao.retrieveAnswersByQuestion(question.getQuestionID());
				
				if(choices.size()>0)
				{
					question.setChoices(choices);
					question.setAnswer(choices.get(0));
				}
			}
			
			professor.setStudents(userdao.retrieveStudentsByProfessor(professor.getUserID()));
		} catch (ClassNotFoundException e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		} catch (SQLException e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
		
		for(int i=0;i<exams.size();i++){
			
			String id = exams.get(i).getExamID();
			
			if(id.substring(id.length()-2).equals("RE")){
				if(type.equals("Practice")){
					exams.remove(i);
					i--;
				}
			}
			else if(id.substring(id.length()-2).equals("PE")){
				if(type.equals("Real")){
					exams.remove(i);
					i--;
				}
			}
		}
		
		for(Exam exam:exams){
			try {
				exam.setQuestions(questiondao.retrieveQuestionsbyExam(exam.getExamID()));
				categories.add(questiondao.retrieveCategoriesbyExam(exam.getExamID()));
			} catch (ClassNotFoundException e) {
				logger.error(e.getMessage(), e);
				e.printStackTrace();
			} catch (SQLException e) {
				logger.error(e.getMessage(), e);
				e.printStackTrace();
			}
		}
		
		professor.setExams(exams);
		
		RequestDispatcher rd=null;
		
		if(type.equals("Practice")){
			rd = request.getRequestDispatcher("Exam_Practice.jsp");
		}
		else{
			rd = request.getRequestDispatcher("Exam_Real.jsp");
		}
		
		request.setAttribute("questions", questions);
		request.setAttribute("categories", categories);
		
		rd.forward(request, response);
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
